﻿using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;

namespace Utility
{
    public class SMS_ZHIQING_Help
    {
        Dictionary<string, string> replayDict = null;

        public Dictionary<string, string> ReplayDict
        {
            get
            {
                if (replayDict == null)
                {
                    replayDict = new Dictionary<string, string>();
                    replayDict["00"] = "多个手机号请求发送成功";
                    replayDict["02"] = "IP限制";
                    replayDict["03"] = "单个手机号请求发送成功";
                    replayDict["04"] = "用户名错误";
                    replayDict["05"] = "密码错误";
                    replayDict["06"] = "编码错误";
                    replayDict["07"] = "发送时间有误";
                    replayDict["08"] = "参数错误";
                    replayDict["09"] = "手机号码有误";
                    replayDict["10"] = "扩展号码有误";
                    replayDict["11"] = "余额不足";
                    replayDict["-1"] = "服务器内部异常";
                    replayDict["REJECT"] = "非法消息内容";
                }
                return replayDict;
            }
        }

        public Dictionary<string, object> SendMessage(string sms_module, string content, string[] mobiles)
        {
            string allow_sms = XmlSettings.Instance.GetValue(sms_module);
            if (allow_sms == "0")
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                result["RETURN_CODE"] = "0";
                result["SMS_CODE"] = "-2";
                result["MESSAGE"] = "权限限制不得发送";
                result["PHONE"] = string.Join(",", mobiles);
                result["TEMPLATE_TEXT"] = content;
                result["RETURN_COMMENT"] = result["MESSAGE"] + ",-2,";
                return result;
            }

            if (allow_sms.ToLower() == "debug")
            {
                mobiles = new string[] { XmlSettings.Instance.GetValue(sms_module, "phone") };
            }
            string userName = XmlSettings.Instance.GetValue("enableSMS_ACCOUNT", "username");
            string password = XmlSettings.Instance.GetValue("enableSMS_ACCOUNT", "password");

            return SendMessage(userName, password, content, mobiles);
        }

        //malong Malocl321
        private Dictionary<string, object> SendMessage(string userName, string password, string content, string[] mobiles)
        {
            string UserName = userName;
            string UserPass = password;
            string Content = content;
            List<string> lst = new List<string>();
            Dictionary<string, object> result = new Dictionary<string, object>();

            foreach (string m in mobiles)
            {
                Match ret1 = Regex.Match(m, @"(\d+)");
                if (!ret1.Success)
                {
                    continue;
                }
                lst.Add(ret1.Groups[1].Value);
            }
            if (lst.Count == 0)
            {
                result["SMS_CODE"] = "-2";
                result["MESSAGE"] = "无法获取电话信息";
                result["PHONE"] = "";
                result["TEMPLATE_TEXT"] = content;
                result["RETURN_COMMENT"] = result["MESSAGE"] + ",-2,";
                result["RETURN_CODE"] = "0";
                return result;
            }

            string mobile = string.Join(",", lst.ToArray());
            string postString = "UserName=" + UserName + "&UserPass=" + UserPass + "&Mobile=" + mobile + "&Content=" + Content;
            byte[] postData = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(postString);

            string url = "http://115.28.112.245:8082/SendMT/SendMessage";
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] responseData = webClient.UploadData(url, "POST", postData);
            string srcString = System.Text.Encoding.GetEncoding("UTF-8").GetString(responseData);
            srcString = srcString.Replace("\n", string.Empty).Replace("\r", string.Empty);
            if (string.IsNullOrWhiteSpace(srcString))
            {
                srcString = "-2,";
            }
            string code = srcString.Split(',')[0];

            if (ReplayDict.ContainsKey(code))
            {
                result["SMS_CODE"] = srcString;
                result["MESSAGE"] = ReplayDict[code];
                result["RETURN_COMMENT"] = result["MESSAGE"] + "," + srcString;
            }
            else
            {
                result["SMS_CODE"] = "-2";
                result["MESSAGE"] = "未知异常";
                result["RETURN_COMMENT"] = result["MESSAGE"] + ",-2,";
            }
            result["PHONE"] = mobile;
            result["TEMPLATE_TEXT"] = content;
            result["RETURN_CODE"] = "0";
            return result;
        }

        public string QueryBalance(string userName, string password)
        {
            string UserName = userName;
            string UserPass = password;
            string postString = "username=" + UserName + "&password=" + password;
            byte[] postData = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(postString);

            string url = "http://115.28.112.245:8082/SendMT/SearchStatus";
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] responseData = webClient.UploadData(url, "POST", postData);
            string srcString = System.Text.Encoding.GetEncoding("UTF-8").GetString(responseData);
            srcString = srcString.Replace("\n", string.Empty).Replace("\r", string.Empty);
            return srcString;
        }
    }
}